package org.wmine.core;


/* TODO Read the List below:
 *	1) Making frame appear center to the window,right now it appears at the top=left corner
 *  2) Currently only one event handler is there - DummyEventHandler
 *	   Write event handler for each menu item
 *	   Put all event handler in this package org.wmine.eventhandlers
 *	   See DummyEventHandler.java 
 */
import javax.swing.*;


import java.awt.*;
import java.awt.event.*;

import org.wmine.eventhandlers.DummyEventHandler;
import org.wmine.eventhandlers.FileMenuEventHandler;
import org.wmine.eventhandlers.HelpMenuEventHandler;
import org.wmine.preprocess.PreprocessPanel;
import org.wmine.algo.AlgorithmPanel;
import org.wmine.visualization.ui.*;
import org.wmine.core.WMineConstants;

/**
 * This is the central GUI classe
 * @author Prafulla (prafulla_t@users.sourceforge.net)
 */
public class MainWindow extends JFrame implements ProjectListener {
	
	/**
	 * Implementation of the Singlton Pattern
	 */
	private static MainWindow mainWindow;
//	JPanel mainPanel;
	public JTabbedPane mainTabbedPane;
	private Container mainContainer;
	
	private JMenuBar menuBar;
    private JMenu fileMenu,helpMenu,importDataMenu;
    private JMenuItem newProjectMenuItem,openExistingProjectMenuItem,exitMenuItem,aboutMenuItem, flatFileMenuItem, databaseFileMenuItem , closeProjectMenuItem;
    
    private JLabel statusBar;
    private static JLabel imageDataMining;
    
    private JMenuItem openDbMenuItem;
	
	/**
	 * Implementation of the Singlton Pattern
	 * All user-classes should access the object of MainWindow using <code>getInstance()</code>
	 * method of this class <br> E.g.
	 * 		<code>
	 *  		 <br> MainWindow obj = MainWindow.getInstance();
	 *			 <br> obj.addPane(new PreprocessPane());
	 *		</code>
	 *	
	 */
	private MainWindow() {


		//Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);
                 		
               		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		setTitle("WMine - Data Mining Tool");
		//Create menu bar
		setupMenuBar();
		//Construct the window
		
		buildWindow();
		//Create toolbar
		buildToolbar();
		//Make sure that all controls are placed properly
		pack();		
	//	mainContainer.remove(image);
	//	mainContainer.add(new JButton("FDF"));
//		setupTabPanels();
		//Show the window
		setVisible(true);

		
		ProjectManager.getUniqueInstance().addProjectListener(this);
	}
	
	
	/**
	 * Constructs the window for the WMine
	 */
	public void buildWindow() {
		
	
	   JPanel mainPanel = new JPanel();
		
		//Change this value to change window size..
		mainPanel.setPreferredSize(new Dimension(800,600));
		
		mainContainer = getContentPane();
		
		mainContainer.setLayout(new BorderLayout());
		mainPanel.setLayout(new BorderLayout());
			
		
		mainTabbedPane = new JTabbedPane();
		
//		setupTabPanels();

		mainPanel.add(mainTabbedPane);
		statusBar = new JLabel("WelCome");
			
		mainContainer.add(mainPanel,BorderLayout.CENTER);
		statusBar = new JLabel("Welcome");
		
		addImage();
		
		
		mainContainer.add(statusBar,BorderLayout.PAGE_END);
		
		
	}
	void addImage(){
		//System.out.println("in Main Window :"+ System.getProperty("user.dir"));
				
		
		String userDir = System.getProperty("user.dir").concat("/FrontImage.jpg");
		Icon imageIcon=new ImageIcon(userDir);
		imageDataMining = new JLabel("",imageIcon,JLabel.CENTER);
		imageDataMining.setPreferredSize(new Dimension(800,600));
		imageDataMining.setVisible(true);
		//System.out.println("in Main Window : adding image");
		mainContainer.add(imageDataMining, BorderLayout.CENTER);
		mainContainer.setVisible(true);
	}
	
	
	/**
	 * Constructs the Menubar
	 */
	protected void setupMenuBar() {
		
		menuBar = new JMenuBar();
		fileMenu = new JMenu("File");
		helpMenu = new JMenu("Help");
		importDataMenu = new JMenu("Import Data");
		
		FileMenuEventHandler fileMenuEventHandler = new FileMenuEventHandler();
		
		DummyEventHandler dummyHandler = new DummyEventHandler();
		
		fileMenu.setMnemonic(KeyEvent.VK_F);
		importDataMenu.setMnemonic(KeyEvent.VK_I);
		
		newProjectMenuItem = new JMenuItem("New Project",KeyEvent.VK_N);
		newProjectMenuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_N, ActionEvent.ALT_MASK));
               
		openExistingProjectMenuItem = new JMenuItem("Open Existing Project",KeyEvent.VK_O);
		openExistingProjectMenuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_O, ActionEvent.ALT_MASK));
        //Now adding dummy event handler for each menu item
        //But it is to be replaced by the proper implementation
        //openMenuItem.addActionListener(dummyHandler);
                
        closeProjectMenuItem = new JMenuItem("Close Project",KeyEvent.VK_C);
        closeProjectMenuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_C, ActionEvent.ALT_MASK));
        
		openDbMenuItem = new JMenuItem("Open DB...",KeyEvent.VK_D);
		openDbMenuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_D, ActionEvent.ALT_MASK));
        openDbMenuItem.addActionListener(dummyHandler);                
	
	        
        flatFileMenuItem = new JMenuItem("Text File",KeyEvent.VK_T);
		flatFileMenuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_T, ActionEvent.ALT_MASK));
    
        
        
        databaseFileMenuItem = new JMenuItem("Database File",KeyEvent.VK_D);
		databaseFileMenuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_D, ActionEvent.ALT_MASK));
    
        
        
        exitMenuItem = new JMenuItem("Exit",KeyEvent.VK_X);
		exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_X, ActionEvent.ALT_MASK));

		//TODO: Do the simillar thing for all menues 
		//Registering fileMenuEventHandler as the ActionLister for all file submenus
		newProjectMenuItem.addActionListener(fileMenuEventHandler);
		openExistingProjectMenuItem.addActionListener(fileMenuEventHandler);
		closeProjectMenuItem.addActionListener(fileMenuEventHandler);
		flatFileMenuItem.addActionListener(fileMenuEventHandler);
		databaseFileMenuItem.addActionListener(fileMenuEventHandler);
        exitMenuItem.addActionListener(fileMenuEventHandler); 
        
        
        
        HelpMenuEventHandler helpMenuEventHandler = new HelpMenuEventHandler();
        aboutMenuItem = new JMenuItem("About",KeyEvent.VK_A);
		aboutMenuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_A, ActionEvent.ALT_MASK));
        aboutMenuItem.addActionListener(helpMenuEventHandler);
                           
	
		
		fileMenu.add(newProjectMenuItem);
		fileMenu.add(openExistingProjectMenuItem);
		fileMenu.add(importDataMenu);
		
		fileMenu.addSeparator();
		fileMenu.add(closeProjectMenuItem);
		fileMenu.add(exitMenuItem);
		
		importDataMenu.add(flatFileMenuItem);		
		importDataMenu.add(databaseFileMenuItem);
		
		helpMenu.add(aboutMenuItem);
		
		menuBar.add(fileMenu);
		menuBar.add(helpMenu);
		
		setJMenuBar(menuBar);
	}	
	
	public void setupTabPanels() {
		 BasicTabPanel basicPanel = new PreprocessPanel();
		//System.out.println("In Set up tab panels");
		mainTabbedPane.addTab(basicPanel.getTitle(),basicPanel.getIcon()
				,basicPanel.buildWindow(),basicPanel.getTooltip());

		basicPanel = new AlgorithmPanel();
		mainTabbedPane.addTab(basicPanel.getTitle(),basicPanel.getIcon()
				,basicPanel.buildWindow(),basicPanel.getTooltip());

		basicPanel = new VisualizationPanel();	
		mainTabbedPane.addTab(basicPanel.getTitle(),basicPanel.getIcon()
				,basicPanel.buildWindow(),basicPanel.getTooltip());
		mainContainer.add(mainTabbedPane);

	}
	
	
	/**
	 * Makes the specified tab as an active tab
	 */
	public void selectTabPanel(int tabIndex) {
			switch(tabIndex) {
			
			case WMineConstants.PREPROCESS_PANEL :
					mainTabbedPane.setSelectedIndex(0);
					break;
			case WMineConstants.MINING_PANEL :
					mainTabbedPane.setSelectedIndex(1);
					break;
			case WMineConstants.VISUALIZATION_PANEL :
					mainTabbedPane.setSelectedIndex(2);
					break;										
		}
	}
	
	/**
	 * Creates the toolbar
	 */
	protected void buildToolbar() {
		JToolBar toolBar = new JToolBar("WMine Toolbar");
	//	toolBar.add(new JButton("Some Button"));
		add(toolBar, BorderLayout.PAGE_START);
	}
	
	/**
	 * Display the string on status bar
	 * @param text String to be displayed
	 */
	 public void showOnStatusBar(String text) {
	 	statusBar.setText(text);
	 }
	
	
	/**
	 *	Singleton Method
	 *	@return Returns the one and only object of the class MainWindow
	 */
	public static MainWindow getInstance() {
		if(mainWindow == null)
			mainWindow = new MainWindow();
		return mainWindow;
	}

	 
	
	/**
	 *	Dummy Main Method - Just for testing
	 *  @param args Expects no parameter
	 */
	public static void main(String[] args) {
		MainWindow mainWindow = MainWindow.getInstance();
		mainWindow.setBounds(150,150,800,600);
	}


	public void onProjectEvent(int eventID) {
		if(eventID == ProjectManager.PROJECT_OPEN_EVENT){
		//	System.out.println("In main Window on ProjectEvent opening");
			mainContainer.remove(imageDataMining);
		
			setupTabPanels();
//			buildWindow();
		}
		if(eventID == ProjectManager.PROJECT_CLOSE_EVENT){
		//	System.out.println("In main Window on ProjectEvent closing");
			//mainContainer.remove(mainTabbedPane);
			MainWindow.getInstance().mainTabbedPane.remove(0);
			MainWindow.getInstance().mainTabbedPane.remove(0);
			MainWindow.getInstance().mainTabbedPane.remove(0);
			addImage();
			//mainContainer.repaint();
			//mainContainer.validate();
			//buildWindow();
			//setupTabPanels();
		}
	}	
}
